package flash.display
{
	import flash.events.Event;
	import flash.events.EventDispatcher;

	public class DisplayObject extends EventDispatcher
	{
		public function DisplayObject()
		{
		}
		public var renderer:RectTransform;
		protected var changePropertys:Object = new Object();
		protected var _alpha:Number = 1;
		public function set alpha(value:Number):void
		{
			_alpha = value;
			changePropertys["alpha"] = value;
		}
		public function get alpha():Number
		{
			return _alpha;
		}
		protected var _height:Number = 100;
		public function set height(value:Number):void
		{
			_height = value;
			changePropertys["height"] = value;
		}
		public function get height():Number
		{
			return _height;
		}
		protected var _scaleX:Number = 1;
		public function set scaleX(value:Number):void
		{
			_scaleX = value;
			changePropertys["scaleX"] = value;
		}
		public function get scaleX():Number
		{
			return _scaleX;
		}
		protected var _scaleY:Number = 1;
		public function set scaleY(value:Number):void
		{
			_scaleY = value;
			changePropertys["scaleY"] = value;
		}
		public function get scaleY():Number
		{
			return _scaleY;
		}
		protected var _scaleZ:Number = 1;
		public function set scaleZ(value:Number):void
		{
			_scaleZ = value;
			changePropertys["scaleZ"] = value;
		}
		public function get scaleZ():Number
		{
			return _scaleZ;
		}
		protected var _visible:Boolean = true;
		public function set visible(value:Boolean):void
		{
			_visible = value;
			changePropertys["visible"] = value;
		}
		public function get visible():Boolean
		{
			return _visible;
		}
		protected var _width:Number = 100;
		public function set width(value:Number):void
		{
			_width = value;
			changePropertys["width"] = value;
		}
		public function get width():Number
		{
			return _width;
		}
		protected var _x:Number = 0;
		public function set x(value:Number):void
		{
			_x = value;
			changePropertys["x"] = value;
		}
		public function get x():Number
		{
			return _x;
		}
		protected var _y:Number = 0;
		public function set y(value:Number):void
		{
			_y = value;
			changePropertys["y"] = value;
		}
		public function get y():Number
		{
			return _y;
		}
		protected var _z:Number = 0;
		public function set z(value:Number):void
		{
			_z = value;
			changePropertys["z"] = value;
		}
		public function get z():Number
		{
			return _z;
		}
		public var name:String;
		
		protected var _parent:DisplayObject;
		protected function set parent(value:DisplayObject):void
		{
			_parent = value;
			changePropertys["parent"] = value;
		}
		public function get parent():DisplayObject
		{
			return _parent;
		}
		private var _stage:Stage;
		protected function set stage(value:Stage):void
		{
			var oldStage:Stage = _stage;
			_stage = value;
			if(oldStage == null && value != null)
			{
				onAddedToStage();
			}
			if(oldStage != null && value == null)
			{
				OnRemovedFromStage();
			}
		}
		public function get stage():Stage
		{
			return _stage;
		}
		internal function setParent(parent:DisplayObject):void
		{
			if(parent != null)
			{
				onAdded();
			}
			else
			{
				onRemoved();
			}
			this.parent = parent;
			stage = parent.stage;
		}
		internal function setStage(stage:Stage):void
		{
			this.stage = stage;
		}
		protected function onAdded():void
		{
			
		}
		protected function onRemoved():void
		{
			
		}
		protected function onAddedToStage():void
		{
			dispatchEvent(new Event(Event.ADDED_TO_STAGE));
		}
		protected function OnRemovedFromStage():void
		{
			var GameObject:Type = importType("UnityEngine.GameObject");
			GameObject.Destroy(renderer);
		}
		public function render():void
		{
			renderX();
			renderY();
			renderZ();
			renderAlpha();
			renderWidth();
			renderHeight();
			renderVisible();
			renderScale();
			changePropertys = new Object();
		}
		protected function renderX():void
		{
			if(changePropertys["x"])
			{
				var pos:Vector2 = renderer.anchoredPosition;
				pos.x = x;
				renderer.anchoredPosition = pos;
			}
		}
		protected function renderY():void
		{
			if(changePropertys["y"])
			{
				var pos:Vector2 = renderer.anchoredPosition;
				pos.y = -y;
				renderer.anchoredPosition = pos;
			}
		}
		protected function renderZ():void
		{
			if(changePropertys["z"])
			{
				var pos:Vector3 = renderer.anchoredPosition;
				pos.z = z;
				renderer.anchoredPosition = pos;
			}
		}
		protected function renderAlpha():void
		{
			if(changePropertys["alpha"])
			{
				var Image:Type = importType("UnityEngine.UI.Image");
				var image:Image = renderer.GetComponent(Image);
				if(image != null)
				{
					image.CrossFadeAlpha(alpha,0,true);
				}
				var Text:Type = importType("UnityEngine.UI.Text");
				var text:Text = renderer.GetComponent(Text);
				if(text != null)
				{
					text.CrossFadeAlpha(alpha,0,true);
				}
			}
		}
		protected function renderWidth():void
		{
			if(changePropertys["width"])
			{
				var size:Vector2 = renderer.sizeDelta;
				size.x = width;
				renderer.sizeDelta = size;
			}
		}
		protected function renderHeight():void
		{
			if(changePropertys["height"])
			{
				var size:Vector2 = renderer.sizeDelta;
				size.y = height;
				renderer.sizeDelta = size;
			}
		}
		protected function renderVisible():void
		{
			if(changePropertys["visible"])
			{
				renderer.gameObject.SetActive(visible);
			}
		}
		protected function renderScale():void
		{
			if(changePropertys["scaleX"])
			{
				var scale:Vector3 = renderer.localScale;
				scale.x = scaleX;
				renderer.localScale = scale;
			}
			if(changePropertys["scaleY"])
			{
				var scale:Vector3 = renderer.localScale;
				scale.y = scaleY;
				renderer.localScale = scale;
			}
			if(changePropertys["scaleZ"])
			{
				var scale:Vector3 = renderer.localScale;
				scale.z = scaleZ;
				renderer.localScale = scale;
			}
		}
	}
}